package com.zznj.xzjc.push;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zznj.xzjc.net.SharedHelper;
import com.zznj.xzjc.utils.NotificationUtil;

import java.util.Map;

import cn.jpush.android.api.CmdMessage;
import cn.jpush.android.api.CustomMessage;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.JPushMessage;
import cn.jpush.android.api.NotificationMessage;
import cn.jpush.android.service.JPushMessageReceiver;

public class PushMessageReceiver extends JPushMessageReceiver {
    private static final String TAG = "PushMessageReceiver";

    @Override
    public void onMessage(Context context, CustomMessage customMessage) {
        Log.e(TAG, "[onMessage] " + customMessage);
        processCustomMessage(context, customMessage);
    }

    @Override
    public void onNotifyMessageOpened(Context context, NotificationMessage message) {
        Log.e(TAG, "[onNotifyMessageOpened] " + message);
        try {
            /**
             * //{"infoId":"1606272844490","infoType":"15","type":"notice"}
             * String messageContent = message.notificationExtras;
             * Map<String,String> msgMap = new Gson().fromJson(messageContent,new TypeToken<Map<String,String>>(){}.getType());
             * String infoId = msgMap.get("infoId");
             * String infoType = msgMap.get("infoType");
             * String type = msgMap.get("type");
             */
            //打开自定义的Activity
//            Intent i = new Intent(context, TestActivity.class);
//            Bundle bundle = new Bundle();
//            bundle.putString(JPushInterface.EXTRA_NOTIFICATION_TITLE,message.notificationTitle);
//            bundle.putString(JPushInterface.EXTRA_ALERT,message.notificationContent);
//            i.putExtras(bundle);
//            //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
//            context.startActivity(i);
            Log.i("通知点击","点击事件");
        } catch (Throwable throwable) {

        }
    }

    @Override
    public void onMultiActionClicked(Context context, Intent intent) {
        Log.e(TAG, "[onMultiActionClicked] 用户点击了通知栏按钮");
        String nActionExtra = intent.getExtras().getString(JPushInterface.EXTRA_NOTIFICATION_ACTION_EXTRA);

        //开发者根据不同 Action 携带的 extra 字段来分配不同的动作。
        if (nActionExtra == null) {
            Log.d(TAG, "ACTION_NOTIFICATION_CLICK_ACTION nActionExtra is null");
            return;
        }
        if (nActionExtra.equals("my_extra1")) {
            Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮一");
        } else if (nActionExtra.equals("my_extra2")) {
            Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮二");
        } else if (nActionExtra.equals("my_extra3")) {
            Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮三");
        } else {
            Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮未定义");
        }
    }

    @Override
    public void onNotifyMessageArrived(Context context, NotificationMessage message) {
        Log.e(TAG, "[onNotifyMessageArrived] " + message);
    }

    @Override
    public void onNotifyMessageDismiss(Context context, NotificationMessage message) {
        Log.e(TAG, "[onNotifyMessageDismiss] " + message);
    }

    @Override
    public void onRegister(Context context, String registrationId) {
        Log.e(TAG, "[onRegister] " + registrationId);
    }

    @Override
    public void onConnected(Context context, boolean isConnected) {
        Log.e(TAG, "[onConnected] " + isConnected);
    }

    @Override
    public void onCommandResult(Context context, CmdMessage cmdMessage) {
        Log.e(TAG, "[onCommandResult] " + cmdMessage);
    }

    @Override
    public void onTagOperatorResult(Context context, JPushMessage jPushMessage) {
//        TagAliasOperatorHelper.getInstance().onTagOperatorResult(context, jPushMessage);
        super.onTagOperatorResult(context, jPushMessage);
    }

    @Override
    public void onCheckTagOperatorResult(Context context, JPushMessage jPushMessage) {
//        TagAliasOperatorHelper.getInstance().onCheckTagOperatorResult(context, jPushMessage);
        super.onCheckTagOperatorResult(context, jPushMessage);
    }

    @Override
    public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {
//        TagAliasOperatorHelper.getInstance().onAliasOperatorResult(context, jPushMessage);
        super.onAliasOperatorResult(context, jPushMessage);
    }

    @Override
    public void onMobileNumberOperatorResult(Context context, JPushMessage jPushMessage) {
//        TagAliasOperatorHelper.getInstance().onMobileNumberOperatorResult(context, jPushMessage);
        super.onMobileNumberOperatorResult(context, jPushMessage);
    }

    //send msg to MainActivity
    private void processCustomMessage(Context context, CustomMessage customMessage) {

        String message = customMessage.message;
        String extras = customMessage.extra;
        Log.i("PUSH_MSG",extras);

        //执法主体：13，权力清单：14，案例库：1，法律法规：2，罪名库：3，科技文献库：4，专家库：5，鉴定机构库：6，专业术语：8，通知公告：15，工作动态：9，产品召回：12，政策发布：11，案例播报：10
        //caseId和caseType{"infoId":"10000134110278","infoType":"15","shortContent":"业务操作更流畅，数据统计更清晰，减轻办公压力，共享大数据办案平台。","title":"湖北正式同步更新","type":"notice"}
        /**
         * {
         *             "msgId": 25,
         *                 "title": "收到互动连线",
         *                 "content": "互动连线:aaaaa",
         *                 "msgType": 3,
         *                 "readStatus": 1,
         *                 "createTime": 1618449064140,
         *                 "districtCode": null,
         *                 "sendOrg": null,
         *                 "receiveOrg": "6199",
         *                 "infoId": 92
         * }
         */
        Map<String,String> extrasMap = new Gson().fromJson(extras,new TypeToken<Map<String,String>>(){}.getType());
        String infoId = extrasMap.get("infoId");
        String msgId = extrasMap.get("msgId");
        String msgType = extrasMap.get("infoType");
        String content = extrasMap.get("content");
        String title = extrasMap.get("title");
        if("1".equals(msgType)){
            int zcfbType = SharedHelper.readValueByKeyForInt(context,"zcfbType");
            if(zcfbType == 1){
                NotificationUtil mNotificationUtils = new NotificationUtil(context);
                mNotificationUtils.showNotification(title,msgId,content,msgType,infoId);
            }
        }else if("2".equals(msgType)){
            int tzggType = SharedHelper.readValueByKeyForInt(context,"tzggType");
            if(tzggType == 1){
                NotificationUtil mNotificationUtils = new NotificationUtil(context);
                mNotificationUtils.showNotification(title,msgId,content,msgType,infoId);
            }
        }else if("3".equals(msgType)){
            //典型案例
            int dxalType = SharedHelper.readValueByKeyForInt(context,"dxalType");
            if(dxalType == 1){
                NotificationUtil mNotificationUtils = new NotificationUtil(context);
                mNotificationUtils.showNotification(title,msgId,content,msgType,infoId);
            }
        }else{
            //指定管辖xsglType
            int xsglType = SharedHelper.readValueByKeyForInt(context,"xsglType");
            if(xsglType == 1){
                NotificationUtil mNotificationUtils = new NotificationUtil(context);
                mNotificationUtils.showNotification(title,msgId,content,msgType,infoId);
            }
        }

        //开启通知栏
//        NotificationUtil mNotificationUtils = new NotificationUtil(context);
//        mNotificationUtils.showNotification();
//            Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);
//            msgIntent.putExtra(MainActivity.KEY_MESSAGE, message);
//            if (!ExampleUtil.isEmpty(extras)) {
//                try {
//                    JSONObject extraJson = new JSONObject(extras);
//                    if (extraJson.length() > 0) {
//                        msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras);
//                    }
//                } catch (JSONException e) {
//
//                }
//
//            }
//            LocalBroadcastManager.getInstance(context).sendBroadcast(msgIntent);
    }

    @Override
    public void onNotificationSettingsCheck(Context context, boolean isOn, int source) {
        super.onNotificationSettingsCheck(context, isOn, source);
        Log.e(TAG, "[onNotificationSettingsCheck] isOn:" + isOn + ",source:" + source);
    }

}
